/*
* @Author: mmall
* @Date:   2017-05-27 17:57:49
* @Last Modified by:   Rosen
* @Last Modified time: 2017-05-28 19:48:16
*/

'use strict';
require('./index.css');
var _mm             = require('util/mm.js');
var _product        = require('service/product-service.js');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _product = require('service/product-service.js');
var Pagination = require('util/pagination/index.js');
var templateIndex   = require('./index.string');
var maxPrice=null;
var minPrice=null;
var page = {
    data : {
        listParam : {
            // categoryId      : _mm.getUrlParam('categoryId') || '',
            maxPrice         : _mm.getUrlParam('maxPrice')    || null,
            minPrice         : _mm.getUrlParam('minPrice')    || null,
            current         : _mm.getUrlParam('pageNum')    || 1,
            pageSize        : _mm.getUrlParam('pageSize')   || 10,
        }
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadList();
    },
    bindEvent : function(){
        var _this = this;
        // 排序的点击事件
        $('#but_price').click(function(){
            var $this = $(this);
            _this.data.listParam.pageNum = 1;
            _this.data.listParam.minPrice=$.trim($('#minPrice').val())
            _this.data.listParam.maxPrice=$.trim($('#maxPrice').val())
            // 重新加载列表
            _this.loadList();
        });
    },
    // 加载list数据
    loadList : function(){
        var _this       = this,
            listHtml    = '',
            listParam   = this.data.listParam,
            $pListCon   = $('.p-list-con');

        $pListCon.html('<div class="loading"></div>');
        // 删除参数中不必要的字段
        listParam.categoryId 
            ? (delete listParam.keyword) : (delete listParam.categoryId);
        // 请求接口
        _product.getSearchRangeQueryList(listParam, function(res){
            listHtml = _mm.renderHtml(templateIndex, {
                list :  res.list
            });
            $pListCon.html(listHtml);
            var totalResultSize = res.totalResultSize;
            var totalPage = res.totalPage;
            var currentPage = res.current;
            minPrice = res.minPrice;
            maxPrice = res.maxPrice;
            _this.loadPagination({
                hasPreviousPage : currentPage > 1,
                prePage         : currentPage - 1,

                hasNextPage     : currentPage < totalResultSize,
                nextPage        : currentPage + 1,

                pageNum         : currentPage,
                pages           : totalPage
            });
        }, function(errMsg){
            _mm.errorTips(errMsg);
        });
    },
    // 加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pageNum){
                _this.data.listParam.current = pageNum;
                _this.data.listParam.maxPrice = maxPrice;
                _this.data.listParam.minPrice = minPrice;
                _this.loadList();
            }
        }));
    }
};
$(function(){
    page.init();
})